package com.backend.server.controller;

import com.backend.server.service.ProjectInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.server.common.BaseResponse;
import com.common.server.common.DeleteRequest;
import com.common.server.common.ErrorCode;
import com.common.server.common.ResultUtils;
import com.common.server.constant.ProjectConstant;
import com.common.server.entity.project.ProjectInfo;
import com.common.server.entity.project.dto.ProjectInfoAddRequest;
import com.common.server.entity.project.dto.ProjectInfoQueryRequest;
import com.common.server.entity.project.dto.ProjectInfoUpdateRequest;
import com.common.server.entity.project.vo.ProjectInfoVO;
import com.common.server.exception.BusinessException;
import com.common.server.exception.ThrowUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: ProjectController
 * Package: com.memory.project_server.controller
 * Description:
 *
 * @Author Memory
 * @Create 2024/12/25 13:34
 * @Version 1.0
 */

@RestController
@Api(value = "项目管理", tags = "项目管理")
@RequestMapping("/project")
@Slf4j
public class ProjectInfoController {
    @Resource
    private ProjectInfoService projectInfoService;

    /**
     * 创建项目
     *
     * @param projectInfoAddRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "创建项目", notes = "创建项目")
    @PostMapping("/add")
    public BaseResponse<Long> addProject(@RequestBody ProjectInfoAddRequest projectInfoAddRequest, HttpServletRequest request) {
        if (projectInfoAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long result = projectInfoService.addProject(projectInfoAddRequest);
        return ResultUtils.success(result, "创建项目成功");
    }

    /**
     * 删除项目
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "删除项目", notes = "删除项目")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProject(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "删除项目请求参数不能为空");
        }
        // 判断是否存在
        int count = projectInfoService.count(new QueryWrapper<ProjectInfo>().eq("id", deleteRequest.getId()));
        ThrowUtils.throwIf(count == 0, ErrorCode.NOT_FOUND_ERROR, "指定删除的项目不存在");
        boolean b = projectInfoService.removeById(deleteRequest.getId());
        return ResultUtils.success(b, "删除项目成功");
    }

    /**
     * 更新项目
     *
     * @param projectInfoUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改项目", notes = "修改项目")
    public BaseResponse<Boolean> updateProject(@RequestBody ProjectInfoUpdateRequest projectInfoUpdateRequest, HttpServletRequest request) {
        if (projectInfoUpdateRequest == null || projectInfoUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoUpdateRequest, projectInfo);
        boolean result = projectInfoService.updateById(projectInfo);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(true, "更新项目成功");
    }


    /**
     * 获取项目列表(管理员)
     *
     * @param projectInfoQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @ApiOperation(value = "获取项目列表(管理员)", notes = "获取项目列表(管理员)")
    public BaseResponse<Page<ProjectInfoVO>> listProjectByPage(@RequestBody ProjectInfoQueryRequest projectInfoQueryRequest) {
        long current = projectInfoQueryRequest.getCurrent();
        long size = projectInfoQueryRequest.getSize();
        Page<ProjectInfoVO> projectInfoVOPage = projectInfoService.getProjectInfoPage(projectInfoQueryRequest);
        log.info("current: {}, size: {}, total: {}", current, size, projectInfoVOPage.getTotal());
        return ResultUtils.success(projectInfoVOPage, "获取项目列表成功");
    }

    /**
     * 获取项目列表
     *
     * @param projectInfoQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @ApiOperation(value = "获取项目列表", notes = "获取项目列表")
    public BaseResponse<Page<ProjectInfoVO>> listProjectVOByPage(@RequestBody ProjectInfoQueryRequest projectInfoQueryRequest, HttpServletRequest request) {
        long current = projectInfoQueryRequest.getCurrent();
        long size = projectInfoQueryRequest.getSize();
        Page<ProjectInfoVO> projectInfoVOPage = projectInfoService.getProjectInfoPage(projectInfoQueryRequest);
        log.info("current: {}, size: {}, total: {}", current, size, projectInfoVOPage.getTotal());
        return ResultUtils.success(projectInfoVOPage,"获取项目列表成功");
    }
}
